//
// Created by Administrator on 2021/11/23.
//

#include "Volume.h"

void Volume::gen_cube(int x, int y, int z, int side, int density) {
    int max_x = x + side, max_y = y + side, max_z = z + side;
    int dim_xy = dim[0] * dim[1];
    for (int k = z; k < max_z; k++) {
        for (int j = y; j < max_y; j++) {
            for (int i = x; i < max_x; i++) {
                data[k * dim_xy + j * dim[0] + i] = density;
            }
        }
    }
}

void Volume::gen_sphere(int x, int y, int z, int radius, int density) {
    int radius2 = radius * radius;
    int dim_xy = dim[0] * dim[1];
    for (int k = 0; k < dim[2]; k++) {
        for (int j = 0; j < dim[1]; j++) {
            for (int i = 0; i < dim[0]; i++) {
                if ((i - x) * (i - x) + (j - y) * (j - y) + (k - z) * (k - z) <= radius2) {
                    data[k * dim_xy + j * dim[0] + i] = density;
                }
            }
        }
    }
}

void Volume::classify() {
    int *linePS = data;
    float *linePD = cData;
    for (int k = 0; k < dim[2]; k++) {
        for (int j = 0; j < dim[1]; j++) {
            for (int i = 0; i < dim[0]; i++) {
                if (linePS[0] <= 100) {
                    //白色
                    linePD[0] = 1.0;
                    linePD[1] = 1.0;
                    linePD[2] = 1.0;
                    linePD[3] = 0.005;
                } else if (linePS[0] <= 200) {
                    //红色
                    linePD[0] = 1.0;
                    linePD[1] = 0.0;
                    linePD[2] = 0.0;
                    linePD[3] = 0.015;
                } else {
                    //黄色
                    linePD[0] = 1.0;
                    linePD[1] = 1.0;
                    linePD[2] = 0.0;
                    linePD[3] = 0.02;
                }
                linePS++;
                linePD += 4;
            }
        }
    }
}

Volume::Volume() {
    data = new int[dim[0] * dim[1] * dim[2]];
    cData = new float[dim[0] * dim[1] * dim[2] * 4];
}

Volume::~Volume() {
    delete[]data;
    delete[]cData;
}

void Volume::generate() {
    gen_cube(0, 0, 0, 200, 100);         //大正方体
    gen_sphere(100, 100, 100, 80, 200);  //球体
    gen_cube(70, 70, 70, 60, 300);       //小正方体
    classify();
}
